package com.etl.dataflow.scheduler.admin.controller;


import com.etl.dataflow.common.response.R;
import com.etl.dataflow.scheduler.admin.entity.DataflowDatasource;
import com.etl.dataflow.scheduler.admin.entity.request.PageRequest;
import com.etl.dataflow.scheduler.admin.service.DataflowDatasourceService;
import com.etl.dataflow.scheduler.admin.validation.Add;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dx
 * @since 2022-04-23
 */
@RestController
@RequestMapping("/admin/ds")
@Validated
public class DataflowDatasourceController implements DefaultController {

    private final DataflowDatasourceService datasourceService;

    public DataflowDatasourceController(DataflowDatasourceService datasourceService) {
        this.datasourceService = datasourceService;
    }

    @PostMapping("/addOrUpdate")
    public R add(@RequestBody @Validated(Add.class) DataflowDatasource datasource) {
        return successOrError(datasourceService.saveOrUpdate(datasource));
    }

    @GetMapping("/remove")
    public R remove(@RequestParam @NotNull Long id) {
        return successOrError(datasourceService.removeById(id));
    }

    @GetMapping("/find")
    public R find(@RequestParam @NotEmpty String id) {
        DataflowDatasource datasource = datasourceService.getById(id);
        return R.success().put("data", datasource);
    }

    @PostMapping("/paging")
    public R paging(@RequestBody @Validated PageRequest pageRequest) {
        return datasourceService.page(pageRequest);
    }

    @GetMapping("/search")
    public R search(@RequestParam String keyword) {
        return datasourceService.search(keyword);
    }
}
